package com.umeox.watch.moto.settings.ui

import android.content.Intent
import android.os.Bundle
import android.os.PowerManager
import android.widget.CompoundButton
import android.widget.RelativeLayout
import android.widget.Switch
import androidx.appcompat.app.AlertDialog
import com.umeox.moto.common.utils.AndroidUtils
import com.umeox.moto.common.utils.SystemUtils
import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity
import com.umeox.watch.moto.settings.R
import com.umeox.watch.moto.settings.utils.MySystemProperties

/**
 * Created by Rambo 2024/9/29/Sunday
 * Describe:
 */
class VolteSettingActivity : ThemeAppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.act_volte_settings)

        findViewById<RelativeLayout>(R.id.volte_config_rl).setOnClickListener {
            val volteConfigIntent = Intent()
            volteConfigIntent.setClassName(
                "com.android.callsettings",
                "com.android.callsettings.VoLTEConfigSettings"
            )
            startActivity(volteConfigIntent)
        }

        val switchVoLTE = findViewById<Switch>(R.id.s_volte)
        switchVoLTE.isChecked = AndroidUtils.isVoLTEEnabled(applicationContext)
        switchVoLTE.setOnCheckedChangeListener(object : CompoundButton.OnCheckedChangeListener {
            private var setCheckFlag = false
            override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
                if (setCheckFlag) return
                val alertDialog: AlertDialog = AlertDialog.Builder(this@VolteSettingActivity, R.style.Theme_AppCompat_DayNight_Dialog)
                    .setMessage(getString(R.string.str_volte_setting_reboot_tip))
                    .setCancelable(false)
                    .setPositiveButton(getString(R.string.str_ok)) { _, _ ->
                        if (isChecked) {
                            MySystemProperties.set("persist.sys.volte.enable", "true")
                            SystemUtils.setVolte(this@VolteSettingActivity, true)
                        } else {
                            MySystemProperties.set("persist.sys.volte.enable", "false")
                        }
                        val pm = mContext.getSystemService(POWER_SERVICE) as PowerManager
                        pm.reboot("setVolteSwitch")
                    }
                    .setNegativeButton(getString(R.string.str_cancel)) { _, _ ->
                        runOnUiThread {
                            setCheckFlag = true
                            if (isChecked) {
                                switchVoLTE.setChecked(false)
                            } else {
                                switchVoLTE.setChecked(true)
                            }
                            setCheckFlag = false
                        }
                    }.create()
                alertDialog.show()
            }
        })
    }
}